#!/bin/bash

err_mes()
{
    echo $1
    exit $2
}

get_input()
{
    [ -z $1 ] && mes="请输入" || mes=$1

    while [ -z $var ]
    do
        [ -z $2 ] && read -p "$mes: " var  \
                  || var=$2
    done

    echo $var
}

is_user()
{
    # if [ -z $1 ]
    # then
        # read -p "" username
    # else
        # username=$1
    # fi

    # if cut -d\: -f1 /etc/passwd | grep -w "$username"
    # then
        # echo "$username 此用户在本系统存在!"
    # else
        # echo "$username 此用户在本系统不存在!"
    # fi

    # while [ -z $username ]
    # do
        # [ -z $1 ] && read -p "请输入验证用户名: " username  \
                  # || username=$1
    # done

    username=$(get_input "请输入验证用户名" $1)

    # cut -d\: -f1 /etc/passwd | grep -w "$username" &> /dev/null \
    egrep -w "^$username" /etc/passwd &> /dev/null      \
        && echo "$username 此用户在本系统存在!"         \
        || echo "$username 此用户在本系统不存在!"
}

is_year()
{
    year=$(get_input "请输入年份" $1)

    # if [ $((year % 400)) -eq 0 ]; then
        # echo "$year 是闰年!"
    # else
        # if [ $((year % 4)) -eq 0 ] && [ $((year % 100)) -ne 0 ]; then
            # echo "$year 是闰年!"
        # else
            # echo "$year 不是闰年!"
        # fi
    # fi

    echo $year | egrep "^[0-9]+$" &> /dev/null      \
        || err_mes "输入年份格式不正确!" 1

    [ $((year % 4)) -eq 0 -a $((year % 100)) -ne 0  \
            -o $((year % 400)) -eq 0 ]              \
        && echo "$year 是闰年!"                     \
        || echo "$year 不是闰年!"
}

html_sort()
{
    [ -z $1 -o ! -e $1 ] && err_mes "请输入合法路径!" 1
    file=$1
    line=$(wc -l $file | cut -d\  -f1)

    for i in $(seq 1 $line)
    do
        l=$(head -n $i $file | tail -n 1)
        l=${l%/*}
        echo ${l##*/}
    done | sort | uniq -c | sort -r
}

# is_user $1
# is_year $1
html_sort ./test

